//
//  EMAppraisalViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/25.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "EMAppraisalViewController.h"
#import "UINavigationController+EMNavi.h"
#import "EMAppraisalCell.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
#import "EMGetAppraisalReqBody.h"
#import "EMGetAppraisalRespBody.h"
#import "UserInfo.h"
#import "EMOrderInfo.h"
#import "EMOrderCell.h"
#import "EMGoodInfo3.h"
#import "UIImageView+WebCache.h"
#import "EMDoCommonViewController.h"
@interface EMAppraisalViewController ()
{
    AppDelegate *app;
    NSArray *orderList;
}
@end

@implementation EMAppraisalViewController
- (void)hideBottomBar
{
    for(UIView *view in self.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
            view.hidden = YES;
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideBottomBar];
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.navigationItem.title = @"商品评价";
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    orderList = [[NSArray alloc] init];
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:button];
    
    [self getAppraisal];
}
- (void)backAction: (UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITableView *messageTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    messageTable.delegate = self;
    messageTable.dataSource = self;
    [self.view addSubview:messageTable];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return orderList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    EMOrderInfo *orderInfo = [[EMOrderInfo alloc] initWithOrderInfo:orderList[section]];
    return orderInfo.m_orderGoodList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"myOrderCell";
    EMOrderInfo *orderInfo = [[EMOrderInfo alloc] initWithOrderInfo:orderList[indexPath.section]];
    EMGoodInfo3 *goodInfo = [[EMGoodInfo3 alloc] initWithOrderGoodInfo:orderInfo.m_orderGoodList[indexPath.row]];
    
    EMOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[EMOrderCell alloc] initWithFrame:CGRectZero];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.goodNameLabel.text = goodInfo.m_name;
    cell.goodDescLabel.text = (NSNull *)goodInfo.m_goodDesc == [NSNull null] ? @"" : goodInfo.m_goodDesc;;
    if ([goodInfo.m_mode isEqualToString:@"0"]) {
        cell.nowPriceLabel.text = [NSString stringWithFormat:@"¥%@", goodInfo.m_nowPrice];
        cell.originPriceLabel.text = @"";
    } else {
        cell.nowPriceLabel.text = [NSString stringWithFormat:@"¥%@", goodInfo.m_nowPrice];
        NSMutableAttributedString *oPrice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%@", goodInfo.m_originPrice]];
        [oPrice addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, goodInfo.m_originPrice.length+1)];
        cell.originPriceLabel.attributedText = oPrice;
    }
    
    [cell.goodImgView sd_setImageWithURL:[NSURL URLWithString:goodInfo.m_imgUrl]];
    cell.commonButton.tag = 20000 + indexPath.section * 100 + indexPath.row;
    [cell.commonButton addTarget:self action:@selector(doCommom:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    EMOrderInfo *orderInfo = [[EMOrderInfo alloc] initWithOrderInfo:orderList[section]];
    
    UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width-20, 30)];
    headerView.backgroundColor = RGB(133, 150, 81);
    headerView.text = [NSString stringWithFormat:@"   %@    订单号:%@    订单状态:%@", orderInfo.m_orderTime, orderInfo.m_orderNumber, [self converseToOrderState:orderInfo.m_orderState]];
    headerView.textColor = [UIColor whiteColor];
    headerView.font = [UIFont fontWithName:HEITILIGHT size:11];
    return headerView;

}
- (NSString *)converseToOrderState:(NSString *)orderStateNum
{
    switch ([orderStateNum intValue]) {
        case 0:
            return @"未付款";
            break;
        case 1:
            return @"已付款";
            break;
        case 2:
            return @"确认订单";
            break;
        case 3:
            return @"配送中";
            break;
        case 4:
            return @"已完成";
            break;
        default:
            return @"";
            break;
    }
}
- (void)doCommom:(UIButton *)sender
{
    NSLog(@"%ld", sender.tag);
    
    NSInteger section = (sender.tag - 20000) / 100;
    NSInteger row = (sender.tag - 20000) % 100;
    
    EMOrderInfo *orderInfo = [[EMOrderInfo alloc] initWithOrderInfo:orderList[section]];
    EMGoodInfo3 *goodInfo = [[EMGoodInfo3 alloc] initWithOrderGoodInfo:orderInfo.m_orderGoodList[row]];
    
    EMDoCommonViewController *controller = [[EMDoCommonViewController alloc] init];
    controller.orderNumber = orderInfo.m_orderNumber;
    controller.goodInfo = goodInfo;
    [self.navigationController pushViewController:controller animated:YES];
    
}
- (void)getAppraisal
{
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMGetAppraisalReqBody *reqBody = [[EMGetAppraisalReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"COMMENT-ORDER-INFO";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMGetAppraisalRespBody *respBody = [EMGetAppraisalRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            orderList = respBody.ORDERINFO;
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
}
@end
